<?php

/*
 * 用户权限控制
 */

namespace Model;

class Wechat
{
            public $Token = 'tripvip';
            
            public $AppID = 'wxafb5cf0cd17eaef9';
            
            public $AppSecret = 'ac8a47188924efa4ff1f09c0b2cfd60d';
            
            public $clientAuthUrl = 'http://m.tripvip.cn/mobile/weixin/auth'; //客户端网页授权回调URL
            
            
            
            private function getAccessToken() //获取access_token
            {
               return  M2('wexin_token')->where(array('id'=>1))->getField('token');        
            }
            
            //创建二维码ticket
            public function getQrcode($sceneID)
            {
                $qrcode = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$sceneID.'}}}';
                $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->getAccessToken();
                $get_url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?";
                $result = $this->request_post($url,$qrcode);
                $jsoninfo = json_decode($result, true);
                $ticket = $jsoninfo["ticket"];
                $qrcode_get_url=$get_url.'ticket='.$ticket;
                 
                $imageInfo = $this->downloadImageFromWeiXin($qrcode_get_url);
                $filename = "qrcode$sceneID.jpg";
                $local_file = fopen('Upload/qrcode/'.$filename, 'w');
                if (false !== $local_file){
                    if (false !== fwrite($local_file, $imageInfo["body"])) {
                        fclose($local_file);
                    }
                }
                return $ticket;
            }
            
            
            public function request_post($postUrl,$curlPost)
            {
            
                curl_close($ch);
                $ch = curl_init(); //初始化curl
                curl_setopt($ch, CURLOPT_URL, $postUrl); //抓取指定网页
                // curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //要求结果为字符串且输出到屏幕上
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //要求结果为字符串且输出到屏幕上
                curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
                curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                $output = curl_exec($ch); //运行curl
                curl_close($ch);
                return $output;
            }
            
            public function getCurl($url)
            {
                //get https的内容
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL,$url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不输出内容
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
                $result =  curl_exec($ch);
                curl_close ($ch);
                return $result;
            }
            
            public function downloadImageFromWeiXin($url)
            {
                $ch = curl_init($url);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_NOBODY, 0);    //只取body头
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                $package = curl_exec($ch);
                $httpinfo = curl_getinfo($ch);
                curl_close($ch);
                return array_merge(array('body' => $package), array('header' => $httpinfo));
            }
          
}




